package com.yifeng.repo.controller.postman.configure;

import com.yifeng.repo.controller.postman.web.PostmanServletRegistrar;
import com.yifeng.repo.controller.postman.work.PostmanManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by daibing on 2023/5/10.
 */
@Configuration
@EnableConfigurationProperties(PostmanProperties.class)
@ConditionalOnClass({PostmanProperties.class})
public class PostmanConfiguration {
    @Value("${spring.application.name}")
    private String applicationName;

    @Resource
    private PostmanProperties properties;

    @Bean(initMethod = "init", destroyMethod = "destroy")
    @ConditionalOnProperty(prefix = "yfcloud.controller.postman", name = "enable-postman-manager")
    public PostmanManager postmanManager() {
        return new PostmanManager(applicationName, properties);
    }

    @Bean
    @ConditionalOnProperty(prefix = "yfcloud.controller.postman", name = "enable-postman-manager")
    public PostmanServletRegistrar postmanServletRegistrar(PostmanManager postmanManager) {
        return new PostmanServletRegistrar(properties.getCallbackPrefix(), postmanManager);
    }

}
